from __future__ import annotations
from typing import Optional
from msgspec import field
from src.dto import MessageStruct
import time

# HTTP 请求头结构体
class OTARequestHeaders(MessageStruct):
    device_id: str = field(name="Device-Id")
    client_id: str = field(name="Client-Id")
    content_type: str = field(name="Content-Type", default="application/json")


# OTA 信息请求结构体 (设备发送给服务器)
class Application(MessageStruct):
    version: str


class Board(MessageStruct):
    name: str


class OTAInfoRequestBody(MessageStruct):
    """OTA 信息请求 - 设备发送给服务器"""

    application: Application
    board: Board


# OTA 信息响应结构体 (服务器返回给设备)
class MQTTConfig(MessageStruct):
    endpoint: str
    client_id: str
    username: str
    password: str
    publish_topic: str
    subscribe_topic: str


class WebSocketConfig(MessageStruct):
    url: str = field(default="")
    token: str = field(default_factory=lambda: "test-token")


class ServerTime(MessageStruct):
    timestamp: int = field(default_factory=lambda: int(time.time() * 1000))
    timezone_offset: int = field(default=480)


class Firmware(MessageStruct):
    version: str = field(default_factory=lambda: "1.8.8")
    url: str = field(default="")


class Activation(MessageStruct):
    code: str
    message: str
    challenge: str


class OTAInfoResponse(MessageStruct):
    """OTA 信息响应 - 服务器返回给设备"""

    mqtt: Optional[MQTTConfig] = field(default=None)
    websocket: Optional[WebSocketConfig] = field(default=None)
    server_time: ServerTime = field(default_factory=ServerTime)
    firmware: Firmware = field(default_factory=Firmware)
    activation: Optional[Activation] = field(default=None)